package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Menu2;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    @Reference
    private MenuService menuService;
    @Reference
    private UserService userService;

    @RequestMapping("getUsername")
    public Result getUsername(Principal principal){

        try {
            String name = principal.getName();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,name);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/getMenuList")
    public Result getMenuList(Principal principal){

        try {
            Map map = new HashMap();
            String username = principal.getName();
            User user = userService.findByUsername(username);
            Set<Role> roles = user.getRoles();
            if(CollectionUtil.isNotEmpty(roles)){
                List<Integer> roleIds = new ArrayList<>();
                for (Role role : roles) {
                    roleIds.add(role.getId());//把用户所有的角色id放到roleIds里面
                    System.out.println(roleIds);
                }
                List<Menu2> menu2s = menuService.getMenuList(roleIds);
                map.put("menuList",menu2s);
            }
            map.put("username",user.getUsername());
            return new Result(true,"成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
    //分页查询用户
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = userService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
            return new Result(true,"查询用户成功",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户失败");
        }
    }
    //分页查询用户
    @RequestMapping("add")
    public Result findPage(@RequestParam("roleIds")Integer [] roleIds, @RequestBody User user) {
        try {
            String password = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.add(roleIds, user);
            return new Result(true,"用户添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"用户添加失败");
        }
    }
    @RequestMapping("edit")
    public Result edit(@RequestParam("roleIds")Integer [] roleIds, @RequestBody User user){

        try {
            String password = bCryptPasswordEncoder.encode(user.getPassword());
            user.setPassword(password);
            userService.edit(roleIds,user);
            return new Result(true,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更新失败");
        }

    }
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id){
        try {
            userService.deleteById(id);
            return new Result(true,"删除用户成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除用户失败");
        }

    }
}
